package gtkwire.widget;

import gtkwire.*;
import gtkwire.listener.ValueChangeListener;
import gtkwire.listener.connector.ValueChangeListenerFilter;

/**
*Retrieve an integer or floating-point number from the user.
*/
public class GtkSpinButton extends GtkEntry
{
	private Object createFlag = null;//used to stop GtkEntry to create a native widget. 
	private double min;
	private double max;
	private double step;

	public GtkSpinButton(double min, double max, double step )
	{
		super();
		this.widgetType = WT_GtkSpinButton;
		this.min = min;
		this.max = max;
		this.step = step;
		createFlag = new Object();
		gtkCreate();
		createFlag = null;
	}

	public GtkSpinButton( String name, GladeKey key )
	{
		super( name, key );
		this.widgetType = WT_GtkSpinButton;
	}

	protected String[] getCreateData()
	{
		if( createFlag == null ) return null;
		String[] msg = { Double.toString( min ), Double.toString( max ), Double.toString( step ) };
		return msg;
	}

	/**
	*Set the value of spin button.
	*/
	public void setValue( double val )
	{
		app().sendCommand( name, GTKWireCommand.SET_VALUE, val );
	}

	/**
	*Returns current value.
	*/
	public double getValue()
	{
		GTKWireMessage answer = app().sendCommandRequest( name, widgetType, GTKWireCommandRequest.GET_VALUE, null );
		return ((Double) answer.getMessageObject()).doubleValue();
	}

	/**
	*Sets entry field to only accept numbers.
	*/
	public void setNumeric( boolean val )
	{
		app().sendCommand( name, GTKWireCommand.SET_NUMERIC, val );
	}
	
	/**
	*Set the precision to be displayed, 0 -20.
	*/
	public void setDigits( int digits )
	{
		app().sendCommand( name, GTKWireCommand.SET_DIGITS, digits );
	}

	/**
	*Sets the minimum and maximum allowable values for spin_button.
	*/
	public void setRange( double min, double max )
	{
		if( min > max )
		{
			double tmp = max;
			max = min;
			min = tmp;
		}
		double[] data = new double[ 2 ];
		data[ 0 ] = min;
		data[ 1 ] = max;
		app().sendCommand( name, GTKWireCommand.SET_RANGE, data );
	}

	/**
	*Determines if a spin button value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.
	*/
	public void setWrap( boolean val )
	{
		app().sendCommand( name, GTKWireCommand.SET_WRAP, val );
	}

	/**
	*Adds listener that is notified always when value is changed.
	*/
	public void addValueChangeListener( ValueChangeListener listener )
	{
		ValueChangeListenerFilter vl = new ValueChangeListenerFilter( listener );
		vl.addFilteringName( name );
		app().addListener( vl );
	}

}//end class